<?php

class Constant_expenseController extends CController
{
	const PAGE_SIZE=10;

	/**
	 * @var string specifies the default action to be 'list'.
	 */
	public $defaultAction='create';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			array('application.filters.LoginFilter'),
			array('application.filters.AuthFilter'),
		);
	}

	/**
	 * Shows a particular model.
	 */
	public function actionShow()
	{
		$this->render('show',array('model'=>$this->loadConstant_expense()));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'show' page.
	 */
	public function actionCreate()
	{
		$model=new Constant_expense;
		if(isset($_POST['Constant_expense']))
		{
			$model->attributes=$_POST['Constant_expense'];
			if($model->save()){
				//$this->render('show',array('model'=>$model,'ret'=>'create','info'=>'添加成功！'));
				//return;
				$this->redirect(array('show','id'=>$model->id,'ret'=>'create','ok'=>'0'));
			}	
		}
		$this->render('create',array('model'=>$model));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'show' page.
	 */
	public function actionUpdate()
	{
		$model=$this->loadConstant_expense();
		if(isset($_POST['Constant_expense']))
		{
			$model->attributes=$_POST['Constant_expense'];
			if($model->save()){
				//$this->render('show',array('model'=>$model,'ret'=>'admin','info'=>'更新成功！'));
				//return;
				$this->redirect(array('show','id'=>$model->id,'ret'=>'admin','ok'=>'1')+$_GET);  
			}
		}
		$this->render('update',array('model'=>$model));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'list' page.
	 */
	public function actionDelete()
	{
		if(isset($_GET['id'])){
			Constant_expense::model()->updateByPk($_GET['id'],array('state'=>1));
			//$this->actionAdmin();
			unset($_GET['id']);
			$this->redirect(array('admin')+$_GET);
		}
		else
			throw new CHttpException(400,'无法获得要删除的信息ID!');
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{	
		$this->search(false);
	}
	
	public function actionSearch(){
		$this->search();
	}
	
	private function search($method=true){
		$criteria=new CDbCriteria();
		$criteria->addCondition('constant_expense.state=0');
		$criteria->join='INNER JOIN trucks ON trucks.id=constant_expense.fk_truck_id';
		$criteria->select='constant_expense.*,trucks.plate_no as plate_no';
		$blur=true;
		if(Yii::app()->getRequest()->isPostRequest){
			if(isset($_POST['keys'])){//页面传来的搜索关键字数组
				$_GET['keys']=$_POST['keys'];
			}
		}
		if(isset($_GET['keys'])){
			if(isset($_GET['keys']['blur'])&&$_GET['keys']['blur']=='0') $blur=false;
			foreach($_GET['keys'] as $key=>$val){
				if(trim($val)=='')
					unset($_GET['keys'][$key]);
				else if($key=='s_date'){//时间查询
					$criteria->addCondition("'{$val}-1' BETWEEN  effect_date and lapse_date");
				}
				else if($key!='blur'){//按照模糊或精确查询
					$blur?$criteria->addSearchCondition($key,$val):$criteria->addCondition("{$key}='{$val}'");
				}
			}
		}
		$model=Constant_expense::model();
		$pages=new CPagination($model->count($criteria));
		$pages->pageSize=Yii::app()->params['page_size']['constant_expense'];
		$pages->applyLimit($criteria);
		
		$sort=new CSort('Constant_expense');
		$sort->attributes=array_merge($model->attributeNames(),array('plate_no'));
		$sort->applyOrder($criteria);

		$models=$model->findAll($criteria);

		$this->render('admin',array(
			'models'=>$models,
			'pages'=>$pages,
			'sort'=>$sort,
			'search'=>$method,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
	 */
	public function loadConstant_expense($id=null)
	{
		if($this->_model===null)
		{
			$criteria=new CDbCriteria();
			$criteria->join='INNER JOIN trucks ON trucks.id=constant_expense.fk_truck_id';
			$criteria->select='constant_expense.*,trucks.plate_no as plate_no';
			$criteria->addCondition('constant_expense.id='.($id!==null ? $id : $_GET['id']));
			$this->_model=Constant_expense::model()->find($criteria);
		}
		return $this->_model;
	}
	
	public function actionCheckConstant(){
		if(Yii::app()->request->isAjaxRequest){
			$condition="fk_truck_id=:truck_id and state=0 and ('{$_POST['effect_date']}-01' BETWEEN effect_date and lapse_date or '{$_POST['lapse_date']}-01' BETWEEN effect_date and lapse_date
						or ('{$_POST['effect_date']}-01'<=effect_date and '{$_POST['lapse_date']}-01'>=lapse_date))";
			if($_POST['constant_id']!='') $condition="id<> {$_POST['constant_id']} and ".$condition;
			$b=Constant_expense::model()->exists(array('condition'=>$condition,
										'params'=>array(':truck_id'=>$_POST['truck_id'])));
			echo $b?1:0;
		}
	}
}
